package com.geek.gkpush.service.order.retry;

import com.geek.gkpush.dal.bean.RetryConfigDO;
import com.geek.gkpush.dal.bean.RetryMsgDO;
import com.geek.gkpush.dal.mapper.RetryConfigMapper;
import com.geek.gkpush.dal.mapper.RetryMsgMapper;
import com.geek.gkpush.framework.retry.RetryMsgPersistFacade;
import com.geek.gkpush.framework.retry.model.RetryConfigModel;
import com.geek.gkpush.framework.retry.model.RetryMsgDTO;
import com.geek.gkpush.framework.retry.model.RetryMsgUpdateModel;
import com.geek.gkpush.util.copy.ShallowCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * <h1>重试消息持久化接口实现类。</h1><br/>
 *
 * <h1><br/><a href="https://u.geekbang.org/subject/arch3/100614201">Java 业务架构实战营：https://u.geekbang.org/subject/arch3/100614201</a></h1><br/><h1><a href="https://gitee.com/ylimhhmily/BizArchPracticeCamp">Java 业务架构实战营代码地址：https://gitee.com/ylimhhmily/BizArchPracticeCamp</a></h1><br/><h1><a href="https://u.geekbang.org/subject/java4th/1001148?source=app_share">高级 Java 工程体系课 2023 版：https://u.geekbang.org/subject/java4th/1001148?source=app_share</a></h1><br/><h1><a href="https://time.geekbang.org/column/intro/100312101?tab=catalog">Dubbo 源码剖析与实战：https://time.geekbang.org/column/intro/100312101?tab=catalog</a></h1>
 *
 * @author hmilyylimh
 * ^_^
 * @version 0.0.1
 * ^_^
 * @date 2023/10/3 18:45
 */
@Component
public class RetryMsgPersistFacadeImpl implements RetryMsgPersistFacade {

    @Autowired
    private RetryMsgMapper retryMsgMapper;
    @Autowired
    private RetryConfigMapper retryConfigMapper;

    @Override
    public void saveRetryMsg(RetryMsgDTO retryMsg) {
        RetryMsgDO retryMsgDO = ShallowCopyUtils.copyBeanProperties(retryMsg, new RetryMsgDO());
        retryMsgMapper.insertObject(retryMsgDO);
    }

    @Override
    public RetryConfigModel getRetryConfig(String bizType) {
        RetryConfigDO dbRetryConfig = retryConfigMapper.queryObjectByBizType(bizType);
        return ShallowCopyUtils.copyBeanProperties(dbRetryConfig, new RetryConfigModel());
    }

    @Override
    public int updateExecStatusByCondition(RetryMsgUpdateModel model) {
        RetryMsgDO reqRetryMsg = new RetryMsgDO();

        reqRetryMsg.setId(model.getId());
        reqRetryMsg.setBizId(model.getBizId());

        reqRetryMsg.setExecTimes(model.getExecTimes());
        reqRetryMsg.setExecStatus(model.getExecStatus());
        reqRetryMsg.setExecResult(model.getExecResult());

        reqRetryMsg.setLastExecTime(model.getLastExecTime());
        reqRetryMsg.setNextExecTime(model.getNextExecTime());

        return retryMsgMapper.updateExecStatusByCondition(reqRetryMsg, model.getSrcExecStatus());
    }

    @Override
    public RetryMsgDTO queryByBizValueBizType(String bizValue, String bizType) {
        RetryMsgDO dbRetryMsg = retryMsgMapper.queryObjectByBizValueBizType(bizValue, bizType);
        return ShallowCopyUtils.copyBeanProperties(dbRetryMsg, new RetryMsgDTO());
    }
}